<template>
  <view class="login_page">
    <cu-custom :bgColor="'bgcolorfff'" :isBack="true">
      <block slot="content" class="toptext">
        <view>{{ allData.title }}</view>
      </block>
    </cu-custom>
    <view class="login">
      <image src="../../static/img/login.png" mode="widthFix" />
      <button open-type="getUserInfo" @getuserinfo="getUserInfo">
        微信登录
      </button>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      type: null,
      id: null,
      userInfo: "",
    };
  },
  onLoad(option) {
    //0 文章 1 直播
    if (option.type && option.id) {
      this.type = option.type;
      this.id = option.id;
    }
  },
  methods: {
    login() {
      return new Promise((reslove, reject) => {
        uni.login({
          success: (res) => {
            reslove(res);
          },
        });
      });
    },
    async openAuth() {
      let { code } = await this.login();
      uni.getUserInfo({
        success: (res) => {
          console.log(res);
          if (res.errMsg == "getUserInfo:ok") {
            try {
              const { signature, encryptedData, iv, userInfo } = res;
              this.$http(
                {
                  url: "/passport/user/ciff/login/weixin",
                  method: "POST",
                  urlType: true,
                },
                {
                  code,
                  signature,
                  userEncryptedData: encryptedData,
                  iv,
                },
                true
              ).then((rs) => {
                if (rs[1].data.code === "000000") {
                  console.log(rs[1].data.data);
                  this.userInfo = rs[1].data.data;
                  uni.setStorageSync("userInfo", rs[1].data.data);
                  uni.showToast({
                    title: "登录成功",
                    icon: "none",
                    duration: 1000,
                  });
                  if (this.type == 0) {
                    uni.redirectTo({
                      url: `/pages/article/article?id=${this.id}&token=${this.userInfo.token}`,
                    });
                  } else if (this.type == 1) {
                    uni.redirectTo({
                      url: `/pages/live/live?id=${this.id}&token=${this.userInfo.token}`,
                    });
                  }
                }
              });
            } catch (error) {}
          }
        },
        fail: (res) => {
          uni.showToast({
            title: "未授权用户信息",
            icon: "none",
            duration: 1000,
          });
        },
      });
    },
    getUserInfo(e) {
      // if (uni.getStorageSync('userInfo')) return //已经有登录过
      this.openAuth();
    },
  },
};
</script>

<style lang="scss" scoped>
.login_page {
  height: 100vh;
  background-color: #ffffff;
}
.login {
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  align-items: center;
  width: 100vw;
  image {
    display: block;
    width: 156rpx;
    margin-top: 172rpx;
  }
  button {
    margin-top: 237rpx;
    width: 674rpx;
    background: rgba(237, 6, 6, 1);
    color: #ffffff;
    font-size: 30rpx;
    font-family: Source Han Sans CN;
    font-weight: 400;
    height: 97rpx;
    line-height: 97rpx;
    border-radius: 0;
    &::after {
      border: none;
    }
  }
}
</style>
